package com.myproject.source;

import java.util.ArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.myproject.adapter.CouponAdapter;
import com.myproject.model.Coupon;
import com.myproject.source.R;


import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;

public class CouponListActivity extends ListActivity {
	static final String KEY_ITEM = "deal"; // parent node
	static final String KEY_ID = "id";
	static final String KEY_TITLE = "title";
	static final String KEY_COUPONCODE = "couponcode";
	static final String KEY_ENDDATE = "expirationDate";
	static final String KEY_IMAGE = "imageUrl";
	static final String KEY_USERID = "userid";
	static final String PREFS_NAME = "MyPrefsFile";
	private static final String PREF_USERID = "userid";
	ArrayList<Coupon> coupons;
	CouponAdapter adapter;
	ListView lv;
	String userid;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.coupon_main);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title_bar);
		// setContentView(R.layout.main);
		// getting intent data
//		Intent in = getIntent();
//		userid = in.getStringExtra(KEY_USERID);

		coupons = new ArrayList<Coupon>();

		WebServiceParser parser = new WebServiceParser();
		
//		Intent in = getIntent();
		SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   
		String userid = pref.getString(PREF_USERID, null);
		Document doc = parser.getDomElement(String.valueOf(userid)); // getting DOM
		NodeList nl = doc.getElementsByTagName(KEY_ITEM);
		// looping through all item nodes <item>
		for (int i = 0; i < nl.getLength(); i++) {
			
			Element e = (Element) nl.item(i);
		
			coupons.add(new Coupon(Integer.parseInt(parser.getValue(e, KEY_ID)),
					parser.getValue(e, KEY_TITLE),
					parser.getValue(e, KEY_COUPONCODE), parser
							.getValue(e, KEY_ENDDATE),  parser
							.getValue(e, KEY_IMAGE)));
		}

		// Adding menuItems to ListView
		setListAdapter(new CouponAdapter(this, coupons));
		

		

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.option_menu, menu);
		return true;
	}

//	protected void onListItemClick(ListView l, View v, int position, long id) {
//		super.onListItemClick(l, v, position, id);
//
//		Deal deal = (Deal) l.getItemAtPosition(position);
//		// String name = deal.getName();
//		// String cost = String.valueOf(deal.getOurPrice());
//		// String description = deal.getDescription();
//		// String deal_id=String.valueOf(deal.getId());
//		// Starting new intent
//		Intent in = new Intent(getApplicationContext(), DealActivity.class);
//		in.putExtra(KEY_TITLE, deal.getName());
//		in.putExtra(KEY_DEALPRICE, String.valueOf(deal.getOurPrice()));
//		in.putExtra(KEY_CITY, deal.getDescription());
//		in.putExtra(KEY_ID, String.valueOf(deal.getId()));
//		in.putExtra(KEY_DETAIL, deal.getDealDetail());
//		in.putExtra(KEY_HOWTOUSE, deal.getDealHowToUse());
//		startActivity(in);
//	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.text1:
			Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG)
					.show();
			break;
		case R.id.text2:
			Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG)
					.show();
			break;
		}
		return true;
	}
}
